<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Category;

class CategoryController extends Controller
{
    use ModelForm;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('商品分类');
            $content->description('管理');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('商品分类');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('商品分类');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(Category::class, function (Grid $grid) {
            // $grid->position_tags('轮播图序号');
            $grid->id('序号');
            $grid->name('商品分类名称');
            $grid->img('商品分类图片')->image();
          //   $grid->desc('资讯详情')->display(function ($desc) {
          //     $data =  str_replace("&nbsp;", "", htmlspecialchars_decode($desc));
          //     return str_limit($data, 30, '...');
          // });
            $grid->column('sketch','商品简述')->display(function ($sketch) {
              return $sketch;
            });//这种方式也可以（第一种方式）
            // $grid->sketch('商品简述')->limit(60);  //这种方式也可以（第二种方式）
            $grid->disableFilter();
            $grid->disableExport();
            // $grid->disableCreateButton();
            // $grid->disablePagination();
            $grid->disableFilter();
            $grid->paginate(10);
            $grid->perPages([10,20,30]);
            $grid->disableRowSelector();
            // $grid->disableActions();
            $grid->actions(function ($actions) {
                // $actions->disableDelete();
                $actions->disableDelete();
            });
        });
    }

protected function form()
{
    return Admin::form(Category::class, function (Form $form) {
           $form->text('name', '商品分类名称')->rules('required|min:2',[
           'required' => '名称不能为空!',
           'min' => '字符必须大于2个!'
          ]);
          $form->image('img', '商品分类图');
          // $form->textarea("sketch","商品分类简述")->rows(10)->rules('required|min:10');
          $form->editor("sketch","商品分类简述")->rules('required|min:2',[
          'required' => '名称不能为空!',
           'min' => '字符必须大于2个!'
      ]);
    });
}
}
